package com.linkallcloud.core.face.message.request;

import com.linkallcloud.core.query.WebQuery;
import com.linkallcloud.core.query.rule.desc.StringRuleDescriptor;

public class QueryFaceRequest extends LoginFaceRequest {
    private static final long serialVersionUID = 6402757110388116334L;

    // 查询条件
    private WebQuery query;

    public QueryFaceRequest() {
        super();
    }

    public QueryFaceRequest(String token, String versn) {
        super(token, versn);
    }

    public QueryFaceRequest(String token) {
        super(token);
    }

    public WebQuery getQuery() {
        return query;
    }

    public void setQuery(WebQuery query) {
        this.query = query;
    }

    public void addRule(StringRuleDescriptor rule) {
        if (rule != null) {
            if (this.query == null) {
                this.query = new WebQuery();
            }
            this.query.addRule(rule);
        }
    }

    /**
     * 是否存在名称是fieldName的rule
     *
     * @param fieldName
     * @return boolean
     */
    public boolean hasRule4Field(String fieldName) {
        if (this.query != null && this.query.getCnds() != null && !this.query.getCnds().isEmpty()) {
            for (StringRuleDescriptor rd : this.query.getCnds()) {
                if (rd.getField().equals(fieldName)) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 得到名称是fieldName的rule
     *
     * @param fieldName
     * @return boolean
     */
    public StringRuleDescriptor getRule4Field(String fieldName) {
        if (this.query != null && this.query.getCnds() != null && !this.query.getCnds().isEmpty()) {
            for (StringRuleDescriptor rd : this.query.getCnds()) {
                if (rd.getField().equals(fieldName)) {
                    return rd;
                }
            }
        }
        return null;
    }

    /**
     * 重新设置名称是fieldName的rule的值
     *
     * @param fieldName
     * @param value
     */
    public void resetRule4Field(String fieldName, String value) {
        if (this.query != null && this.query.getCnds() != null && !this.query.getCnds().isEmpty()) {
            for (StringRuleDescriptor rd : this.query.getCnds()) {
                if (rd.getField().equals(fieldName)) {
                    rd.setData(value);
                }
            }
        }
    }

}
